<?php
namespace Wechat\Controller;

use Wechat\Controller\WxCustomerBaseController; 

class IndexController extends WxCustomerBaseController {
    
    protected $bookCatModel;
	protected $bookModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->bookModel = D("Book/Book");
		$this->bookCatModel = D("Book/BookCat");
	}
	
    // 首页
	public function index() {
    	$this->display(":index");
		return;
    }

	// 列表页
	public function lists() {
		////// PARAM MAP (NO MONEY)
		$paramMapArray = ["pi" => "province_id", "tp" => "type"];
		
	    ////// CATEGORY
		// $bookCatId
	    $bookCatId = I('get.cid', 0, 'intval');

	    // get cat
	    $bookCat = $this->bookCatModel->where(array("id" => $bookCatId))->find();
	    if (empty($bookCat)) {
	        header('HTTP/1.1 404 Not Found');
	        header('Status:404 Not Found');
	        
	    	$msg = 'cat id: ' . $bookCatId . ' NOT FOUND';
	        $this->assign("msg", $msg);
	        $this->display("../Public:404");
	        return;
	    }
	    $bookCat['seo_title'] = "活动大厅";                               // hard code+++
	    $bookCat['seo_keywords'] = "活动关键字";
	    $bookCat['seo_description'] = "活动描述";

		$this->assign($bookCat);
	    
	    // bookBaseUrl
	    $bookBaseUrl = U('Book/Index/lists');						// +++
		
		////// FILTER ATTRS
		$result = M("Area")->where(array("pid" => 0))->select();
		$areaArray = [
			["db_value" => "", "name" => "不限"]
		];
		foreach ($result as $k => $v) {
			array_push($areaArray, ["db_value" => $v['id'], "name" => $v['name']]);
		}
		$attrs = [
			[
				"db_field" => "pi",
				"name" => "地区",
				"options" => $areaArray
			], [
				"db_field" => "tp",
				"name" => "类型",
				"options" => [
					[
						"db_value" => "",
						"name" => "不限"
					], [
						"db_value" => "1",
						"name" => "债权"
					], [
						"db_value" => "2",
						"name" => "债权包"
					]
				]
			], [
				"db_field" => "mr",
				"name" => "金额",
				"options" => [
					[
						"db_value" => "",
						"name" => "不限"
					], [
						"db_value" => "1",
						"name" => "10万以下"
					], [
						"db_value" => "2",
						"name" => "10-100万"
					], [
						"db_value" => "3",
						"name" => "100万以上"
					]
				]
			],
		];

		$this->assign("attrs", $attrs);
// 		dump($attrs);

	    ////// WHERE
	    // $filterArray and $moneyRangeStrArray
	    $filterArray = ["pi", "tp"];									// base filter
	    $moneyRangeStrArray = [
	    	1 => "-99999",
	    	2 => "100000-999999",
	    	3 => "1000000-",
	    ];

	    // make $params and $where
	    $params = array();
	    $where = "cid = $bookCatId";
		foreach ($_GET as $k => $v) {
			if (in_array($k, $filterArray)) {
				$params[$k] = $v;
				$where .= " and $paramMapArray[$k] = $v";
			}
		}

		// handle $where money range and auto mapper money
		if (isset($_GET['mr'])) {
			$params['mr'] = $_GET['mr'];
			$moneyRangeArray = split('-', $moneyRangeStrArray[$_GET['mr']]);		// base filter
			if ($moneyRangeArray[0]) {
				$where .= " and money >= $moneyRangeArray[0]";
			}
			if ($moneyRangeArray[1]) {
				$where .= " and money <= $moneyRangeArray[1]";
			}
		}
		
		$where .= " and status = 1";

		$this->assign("params", $params);
		$this->assign("bookBaseUrl", $bookBaseUrl);
		$this->assign("where", $where);
// 	dump($params);
// 	dump($where);
		
		$this->display(":book-list");
		return;
	}

    // 单页（内容页）
	public function show() {
		$bookCatId = I('get.cid', 0, 'intval');
		$id = I('get.id', 0, 'intval');

		////// GET OBJ
		// $where
		$where = array();
		$where['id'] = array('eq', $id);
		$where['status'] = array('eq', 1);
		
		// view updated
		$this->bookModel->where($where)->setInc('view_count', 1);

		// get obj
		$book = $this->bookModel->where($where)->find();
		if (empty($book)) {
			header('HTTP/1.1 404 Not Found');
			header('Status:404 Not Found');
			
			$msg = 'obj id: ' . $id . ' NOT FOUND';
			$this->assign("msg", $msg);
			$this->display("../Public:404");
			return;
		}

		$this->assign($book);
		
		////// GET PREV & NEXT
		$prev = $this->bookModel->where("id < $id and cid = $bookCatId and status = 1")->order('id desc')->limit('1')->find();
		$this->assign('prev', $prev);
		$next = $this->bookModel->where("id > $id and cid = $bookCatId and status = 1")->order('id asc')->limit('1')->find();
		$this->assign('next', $next);

		////// customer
		$this->assign('customer', $this->customer);

		$this->display(":book-single");
		return;
	}
	
    // 填写页面
	public function fillreg() {
	    // customer
// 	    if (!$this->customer) {
// 			$this->error(L('NO_REGISTER'), U('Customer/Login/register', array('redirect'=>base64_encode($_SERVER['HTTP_REFERER']))));
// 	    }
	    $this->assign('customer', $this->customer);
// 	    dump($this->customer);
	    
	    // book and code
		$tid = I('get.tid', 0, 'intval');
		$codeId = I('get.codeId', 0, 'intval');
		
		$book = M('Book')->where("id = $tid")->find();
		$this->assign('tid', $tid);
		$this->assign('book', $book);
		$this->assign('codeId', $codeId);
		
    	$this->display(":reg-fill");
		return;
    }
	
    // 确认页面
	public function confirm() {
    	$this->display(":confirm");
		return;
    }

}
